#include "DarkFoodCard.h"
#include <QPainter>
#include <QLinearGradient>
#include <QFont>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QDebug> // For debugging

DarkFoodCard::DarkFoodCard(QWidget* parent)
    : QWidget(parent)
{
    // setAttribute(Qt::WA_TranslucentBackground); // Keep if needed for custom paintEvent
    setAttribute(Qt::WA_StyledBackground, true); // Needed for QSS background
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); // Adjust sizing
    setMinimumSize(sizeHint()); // Set minimum size based on hint

    setupUi();
}

void DarkFoodCard::setupUi() {
    // Create Widgets
    // imageLabel = new QLabel("Image Placeholder"); // Placeholder for image
    // imageLabel->setFixedSize(160, 120); // Example size
    // imageLabel->setAlignment(Qt::AlignCenter);
    // imageLabel->setStyleSheet("background-color: #333; color: #888; border-radius: 8px;");

    nameLabel = new QLabel("商品名称");
    nameLabel->setObjectName("cardNameLabel");
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); // Left align text
    nameLabel->setWordWrap(true);

    priceLabel = new QLabel("￥ 0.00");
    priceLabel->setObjectName("cardPriceLabel");
    priceLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); // Left align price

    // --- Quantity Controls ---
    decreaseButton = new QPushButton("-");
    decreaseButton->setObjectName("cardDecreaseButton");
    decreaseButton->setFixedSize(25, 25); // Make buttons square-ish
    decreaseButton->setCursor(Qt::PointingHandCursor);

    quantityLabel = new QLabel("1"); // Initial quantity
    quantityLabel->setObjectName("cardQuantityLabel");
    quantityLabel->setMinimumWidth(30); // Ensure space for quantity
    quantityLabel->setAlignment(Qt::AlignCenter);

    increaseButton = new QPushButton("+");
    increaseButton->setObjectName("cardIncreaseButton");
    increaseButton->setFixedSize(25, 25);
    increaseButton->setCursor(Qt::PointingHandCursor);
    // --- End Quantity Controls ---

    addToCartButton = new QPushButton("加入购物车");
    addToCartButton->setObjectName("cardAddToCartButton");
    addToCartButton->setCursor(Qt::PointingHandCursor);

    // Layout
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(12, 12, 12, 12); // Use padding from QSS now
    mainLayout->setSpacing(6); // Reduce spacing between elements

    // mainLayout->addWidget(imageLabel, 0, Qt::AlignCenter); // Add image label if used
    mainLayout->addWidget(nameLabel);
    mainLayout->addWidget(priceLabel);

    // Layout for quantity and button (bottom row)
    QHBoxLayout* bottomLayout = new QHBoxLayout();
    bottomLayout->setSpacing(8); // Adjust spacing in the bottom row
    bottomLayout->addWidget(new QLabel("数量:"), 0, Qt::AlignVCenter); // Align label vertically

    // Quantity controls layout
    QHBoxLayout* quantityControlLayout = new QHBoxLayout();
    quantityControlLayout->setContentsMargins(0, 0, 0, 0); // No extra margins
    quantityControlLayout->setSpacing(3); // Spacing between -, label, +
    quantityControlLayout->addWidget(decreaseButton);
    quantityControlLayout->addWidget(quantityLabel);
    quantityControlLayout->addWidget(increaseButton);
    quantityControlLayout->addStretch(0); // Keep controls compact

    bottomLayout->addLayout(quantityControlLayout); // Add controls group
    bottomLayout->addStretch(1); // Push button to the right
    bottomLayout->addWidget(addToCartButton, 0, Qt::AlignVCenter); // Align button vertically
    mainLayout->addLayout(bottomLayout);

    // Connect button signals
    connect(addToCartButton, &QPushButton::clicked, this, &DarkFoodCard::onAddToCartClicked);
    connect(increaseButton, &QPushButton::clicked, this, &DarkFoodCard::onIncreaseQuantity);
    connect(decreaseButton, &QPushButton::clicked, this, &DarkFoodCard::onDecreaseQuantity);

    // Styling is now handled by external QSS files loaded in MainWindow
}


void DarkFoodCard::setDrinkDetails(const QString& name, const QString& category, float price, int drinkId) {
    m_drinkId = drinkId;
    m_drinkName = name; // Store name
    m_drinkPrice = price; // Store price

    nameLabel->setText(name);
    priceLabel->setText(QString("￥ %1").arg(price, 0, 'f', 2));
    // categoryLabel->setText(category); // If category label exists
    m_quantity = 1; // Reset internal quantity
    quantityLabel->setText(QString::number(m_quantity)); // Update label
    // TODO: Set image based on name/category/id if imageLabel exists
}

void DarkFoodCard::onAddToCartClicked() {
    // Use internal quantity
    qDebug() << "Add to cart button clicked for drink ID:" << m_drinkId << "Quantity:" << m_quantity;
    if (m_drinkId != -1 && m_quantity > 0) {
        emit addToCartClicked(m_drinkId, m_quantity);
    } else {
         qDebug() << "Invalid drink ID or quantity";
    }
}


void DarkFoodCard::paintEvent(QPaintEvent* event) {
     // Option 1: Let QSS handle everything (remove this function if no custom drawing)

     // Option 2: Keep custom background drawing if QSS is not sufficient or for effects
     QPainter p(this);
     p.setRenderHint(QPainter::Antialiasing);
     // Draw custom background (e.g., gradient) - ensure WA_StyledBackground is false if using this
     // QLinearGradient grad(rect().topLeft(), rect().bottomRight());
     // grad.setColorAt(0, QColor("#23272e"));
     // grad.setColorAt(1, QColor("#1a1d23"));
     // p.setBrush(grad);
     // p.setPen(Qt::NoPen);
     // p.drawRoundedRect(rect().adjusted(1, 1, -1, -1), 15, 15); // Adjust rect slightly if border is drawn by QSS

     // Call base class paintEvent if needed, or handle all painting here.
     QWidget::paintEvent(event); // Important if using QSS for background/border
}


// --- New Slots for Quantity Buttons ---

void DarkFoodCard::onIncreaseQuantity() {
   if (m_quantity < 99) { // Set max quantity limit
       m_quantity++;
       quantityLabel->setText(QString::number(m_quantity));
   }
}

void DarkFoodCard::onDecreaseQuantity() {
   if (m_quantity > 1) { // Set min quantity limit
       m_quantity--;
       quantityLabel->setText(QString::number(m_quantity));
   }
}

// Remove old animation functions:
// startAnimation(), stopAnimation(), onTimeout(), drawCoffee(), drawCake(), etc.